﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Game.Facade;
using Game.Kernel;
using Game.Utils;
using Game.Entity.Accounts;
using Game.Entity.Treasure;

namespace Game.Card.Menu
{
    public partial class BuyInfo : AdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SiteMaster.Menu = 1;
            if(!IsPostBack)
            {
                //获取登录信息
                UserTicketInfo user = userTicket;

                //获取游戏豆信息
                UserCurrencyInfo currency = FacadeManage.aideTreasureFacade.GetUserCurrencyInfo(user.UserID);
                lbCurrency.Text = currency != null ? Convert.ToInt32(currency.Currency).ToString() : "0";

                //获取房卡信息
                UserRoomCard card = FacadeManage.aideTreasureFacade.GetUserRoomCard(user.UserID);
                lbRoomCard.Text = card != null ? card.RoomCard.ToString() : "0";

                //获取购买产品
                int id = GameRequest.GetQueryInt("id", 0);
                RoomCardConfig config = FacadeManage.aideTreasureFacade.GetRoomCardConfig(id);
                if(config == null)
                {
                    return;
                }

                lbExchCard.Text = config.RoomCard.ToString();
                lbPayCurrency.Text = Convert.ToInt32(config.Currency).ToString();
            }
        }

        /// <summary>
        /// 购买
        /// </summary>
        protected void btnBuy_Click(object sender, EventArgs e)
        {
            int id = GameRequest.GetQueryInt("id", 0);
            if(id <= 0)
            {
                ShowInfo("抱歉，请重新选择购买产品");
                return;
            }
            string password = CtrlHelper.GetText(txtPassword);
            if(string.IsNullOrEmpty(password))
            {
                ShowInfo("抱歉，银行密码不能为空");
                return;
            }
            Message msg = FacadeManage.aideTreasureFacade.BuyRoomCard(userTicket.UserID, TextEncrypt.EncryptPassword(password), id, GameRequest.GetUserIP());
            if(msg.Success)
            {
                Response.Redirect("/Menu/BuySuccess.aspx");
            }
            else
            {
                ShowInfo(msg.Content);
            }
        }
    }
}